﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;
using TrainingsApp.Logic;
using TrainingsApp.WebModel.Route;
using TrainingsApp.WebModel.Storage;

namespace TrainingsApp.IPADApp.UserControls.Main {
    public partial class UC_Login : ControlBase {
        protected void Page_Load(object sender, EventArgs e) {

            string username = string.Empty;
            string pwd = string.Empty;


            if (!this.IsPostBack) {
                UserCookieFacade.GetPasswordAndUsernameForCookie(out username, out pwd);
                if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(pwd)) {
                    this.txtUsername.Text = username;
                    this.txtPassword.Text = pwd;

                    //btnLogin_Click(null, null);
                }
                var entity = TS_SystemInfoSetting_Logic.View("SYSTEMACTION");
                if (entity != null) { this.CKEditor1.Text = entity.ConfigValue; }
            }

        }

        protected void btnLogin_Click(object sender, EventArgs e) {



            string account = this.txtUsername.Text.Replace("'", "''");
            string password = this.txtPassword.Text.Replace("'", "''");

            UserContext user = null;

            try {

                user = Sys_Permission_Logic.UserLogin(account, password);
                //user = Sys_Permission_Logic.UserLogin(account, string.IsNullOrEmpty(password) ? password : WisDragon.Framework.Utility.DesSecurity.DesEncrypt(password));
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

            if (user == null) {
                this.txtUsername.CssClass = "password passwordfail";
                this.txtUsername.ToolTip = "登录失败，请重新登录!";
            }
            else {


                int savePwd = ZJFramework10.Utility.FormManager.GetValue<int>("passwordChk", 0);
                user = Sys_Permission_Logic.GetUserInfo(user.UserName);
                if (savePwd == 1)
                    UserCookieFacade.SetSavePasswordToCookie(user.UserName, password);
                Session["CurContext"] = user;
                UserCookieFacade.SetLoginCookie(user.UserName, password);
                Response.Redirect(this.CurPage.RouteHelper.Route("index"));
            }

        }
    }
}